﻿#include "getdevinfos.h"

#include <QStorageInfo>
#include <QNetworkInterface>
#include <Windows.h>
#include <QSettings>
#include <QDomDocument>
#include <QJsonDocument>
#include <QTreeView>

#define GB 1024 / 1024 / 1024


GetDevInfos::GetDevInfos()
{
    qDebug() << getSoftware();
    QTreeView tree;

}

QString GetDevInfos::getLoginUserName()
{
    return qgetenv("USERNAME");
}

QString GetDevInfos::getHostName()
{
    return QHostInfo::localHostName();
}

QString GetDevInfos::getOsVersion()
{
    typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
    LPFN_ISWOW64PROCESS fnIsWow64Process;
    BOOL bIsWow64 = FALSE;
    fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
    if (NULL != fnIsWow64Process)
    {
        fnIsWow64Process(GetCurrentProcess(),&bIsWow64);
    }
    QString sysBit = "unknown";
    if(bIsWow64)
        sysBit = QStringLiteral("64位");
    else
        sysBit = QStringLiteral("32位");

    return QSysInfo::prettyProductName() + " " + sysBit;
}

QString GetDevInfos::getOsMemory()
{
    QList<QStorageInfo> list = QStorageInfo::mountedVolumes();
    qDebug() << "[" << __FUNCTION__ <<__LINE__ << "] :" <<  list.length();
    for(QStorageInfo& si : list)
    {
        qDebug() << "Name: " << si.name();
        qDebug() << "Block Size: " << si.blockSize();
        qDebug() << "Bytes Avaliable: " << si.bytesAvailable() / GB;
        qDebug() << "Bytes Free: " << si.bytesFree() / GB;
        qDebug() << "Bytes Total: " << si.bytesTotal() / GB;
        qDebug() << "Display Name: " << si.displayName();
        qDebug() << "File System Type: " << si.fileSystemType();
        qDebug() << "Is ReadOnly: " << si.isReadOnly();
        qDebug() << "Is Root: " << si.isRoot();
        qDebug() << endl;
    }
    return "";

}

QString GetDevInfos::getIPv4()
{
    QHostInfo info = QHostInfo::fromName(getHostName());
    if(!info.addresses().isEmpty())
    {
        foreach (QHostAddress address, info.addresses())
        {
            if(address.protocol() == QAbstractSocket::IPv4Protocol)
                return address.toString();
        }
    }
    return "";

}

QString GetDevInfos::getMac()
{
    if(QNetworkInterface::allInterfaces().length() > 0)
    {
        foreach (QNetworkInterface networkinterface, QNetworkInterface::allInterfaces())
        {
            return networkinterface.hardwareAddress();
        }
    }
    return "";
}

QString GetDevInfos::getSoftware()
{
    QSettings settings(strUninstall, QSettings::NativeFormat);
    QStringList regGroups = settings.childGroups();
    foreach (QString regItem , regGroups)
    {
        settings.beginGroup(regItem);
//        QStringList regAllKeys = settings.allKeys();
        QString displayName = settings.value("DisplayName").toString();
        QString uninstallString = settings.value("UninstallString").toString();
        if(!displayName.isEmpty())
        {
            qDebug() << "[" << __FUNCTION__ <<__LINE__ << "] :" <<  displayName << uninstallString;
        }

        settings.endGroup();
    }
    return "";
}

QString GetDevInfos::getCPU()
{
    QSettings settings("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", QSettings::NativeFormat);
    QString  value = settings.value("ProcessorNameString").toString();
    return value;
}
